package com.demo.redis;


/**
 * @author wangfengchen
 */

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 声明该类为配置类
@Configuration
// 让Spring识别RedisProperty里面定义的配置信息
@EnableConfigurationProperties(RedisProperty.class)
public class RedisConfiguration {


  private final String SCHEMA = "redis://";

  @Autowired
  private RedisProperty redisProperty;


  @Bean
  @ConditionalOnProperty(prefix = "redis", name = "mode", havingValue = "single")
  public RedissonClient singleRedissonClient() {
    Config config = new Config();
    String address = SCHEMA + redisProperty.getAddress() + ":" + redisProperty.getPort();
    config.useSingleServer().setAddress(address).setUsername(redisProperty.getUsername()).setPassword(redisProperty.getPassword()).setSslEnableEndpointIdentification(redisProperty.isSsl())
        .setConnectionPoolSize(redisProperty.getPoolSize()).setConnectionMinimumIdleSize(redisProperty.getConnectionMinimumIdleSize())
        .setIdleConnectionTimeout(redisProperty.getIdleTimeout()).setDatabase(redisProperty.getDatabase());
    config.setCodec(new JsonJacksonCodec());
    return Redisson.create(config);
  }

  @Bean
  @ConditionalOnProperty(prefix = "redis", name = "mode", havingValue = "sentinel")
  public RedissonClient sentinelRedissonClient() {
    Config config = new Config();
    String address = redisProperty.getAddress();
    String[] splitAddress = address.split(",");
    for (int i = 0; i < splitAddress.length; i++) {
      splitAddress[i] = SCHEMA + splitAddress[i];
    }
    config.useSentinelServers().addSentinelAddress(splitAddress).setDatabase(redisProperty.getDatabase())
        .setMasterName(redisProperty.getMasterName()).setPassword(redisProperty.getPassword());
    config.setCodec(new JsonJacksonCodec());
    return Redisson.create(config);
  }

  @Bean
  @ConditionalOnProperty(prefix = "redis", name = "mode", havingValue = "cluster")
  public RedissonClient clusterRedissonClient() {
    Config config = new Config();
    String address = redisProperty.getAddress();
    String[] splitAddress = address.split(",");
    for (int i = 0; i < splitAddress.length; i++) {
      splitAddress[i] = SCHEMA + splitAddress[i];
    }
    config.useClusterServers().addNodeAddress(splitAddress).setUsername(redisProperty.getUsername()).setPassword(redisProperty.getPassword())
        .setSslEnableEndpointIdentification(redisProperty.isSsl()).setMasterConnectionMinimumIdleSize(redisProperty.getConnectionMinimumIdleSize())
        .setIdleConnectionTimeout(redisProperty.getIdleTimeout());
    config.setCodec(new JsonJacksonCodec());
    return Redisson.create(config);
  }


}